<?php
/* SVN FILE: $Id: app_controller.php 256 2010-06-18 11:45:11Z gregory.rome $ */
/**
 * Application Controller.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * cBBweb :  digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource    app_controller.php
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @version       $Revision: 256 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-18 11:45:11 +0000 (Fri, 18 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Application Controller.
 *
 * @package       cbbweb
 */
class AppController extends Controller {
	var $helpers = array (
		'Menu',
		'Html',
		'Form',
		'Javascript',
		'Number',
		'Paginator',
		'Session'
	);
	var $components = array (
		'RequestHandler',
		'Auth',
		'Acl',
		'Cookie',
		'Session'
	);
	var $locale;
	var $championship;
	var $user;

	function beforeFilter() {
		parent::beforeFilter();

		$this->locale = Configure :: read('Config.language');

		$this->championship = $this->Session->read('Prefs.Championship');
		if (!$this->championship) {
			if (isset ($this->data['AppController']['Championship'])) {
				$this->championship = $this->data['AppController']['Championship'];
			} else {
				$cookie = $this->Cookie->read('Preferences');
				$this->championship = $cookie['championship'];
				$this->Session->write('Prefs.Championship', $this->championship);
			}
		}

		$this->Auth->actionPath = 'controllers/';
		$this->Auth->authorize = 'actions';
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
		$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
		$this->Auth->allowedActions = array('display','index', 'view');

		//$this->Auth->allowedActions = array ('*');
		$this->user['User'] = $this->Session->read('Auth.User');
	}

	function beforeRender() {
		parent::beforeFilter();

		if (isset ($this->data['AppController']['Championship'])) {
			$this->championship = $this->data['AppController']['Championship'];

			$cookie = array ();
			$cookie['championship'] = $this->data['AppController']['Championship'];
			$this->Cookie->write('Preferences', $cookie, true, '+2 weeks');
			$this->Session->write('Prefs.Championship', $this->data['AppController']['Championship']);
		}

		$this->set('locale', $this->locale);

		$user = $this->Session->read('Auth.User');
		if ($user) {
			$this->set('user', $user['username']);
		}
		Configure :: load('options');
		$this->set('ganalytics', Configure :: read('GoogleAnalytics.code'));

		/* Define current championship -------------------------------------- */
		App :: import('Model', 'Championship');
		$Championship = new Championship;
		$championships_list = $Championship->find('list', array (
			'fields' => array (
				'ID',
				'strName'
			)
		));
		$this->set('championships_list', $championships_list);
		$prefs = $this->Session->read('Prefs');
		$this->set('current_championship', $prefs['Championship']);


	}

	function changeDbSource($database = 'default') {
		$db = ConnectionManager :: getInstance();
		$connected = $db->getDataSource($database);
		if ($connected->isConnected()) {
			return true;
		}
		return false;
	}

	function debug($data) {
		echo '<pre>';
		print_r($data);
		echo '<pre>';
		exit(0);
	}

  function isUploadedFile($file) {
    if ((isset($file['error']) && $file['error'] == 0) ||
    (!empty($file['tmp_name']) && $file['tmp_name'] != 'none'))
    {
      return is_uploaded_file($file['tmp_name']);
  	} else {
      return false;
    }
  }
}
?>
